import { clearAll,downArea } from './riverWater'
import { removeGate,removeWaterSystem,createFlood,openGate,closeGate} from './addFloodgate'


export default class Effect5 {
  constructor(viewer,type){
    //type 0 建成后  1 建成前
    this.viewer = viewer
    this.type = type
    this.waterEffect = null

    this.highEntity = null
    this.highValue = type === 0 ? 41 : 40
    this.lowEntity = null
    this.lowEntity1 = null
    this.lowValue = type === 0 ? 41.15 : 41

    this.yanmoEntity = null

    this.shuiweiTimer = null
    this.xiangjiangTimer = null

    this.highPop = null
    this.lowPop = null
    this.lowPop1 = null

    this.start()
  }

  start(){
    if(this.highPop) return
    // this.highEntity = this.viewer.entities.add({
    //   name:'1',
    //   position:Cesium.Cartesian3.fromDegrees(113.12425753259735,27.854658401763434,65),
    //   label:{
    //     text: `${this.highValue}m`,
    //     font:'22px sans-serif',
    //     fillColor:Cesium.Color.CRIMSON 
    //   }
    // })

    // this.lowEntity = this.viewer.entities.add({
    //   name:'2',
    //   position:Cesium.Cartesian3.fromDegrees(113.12412411899265,27.853921627676886,65),
    //   label:{
    //     text:  `${this.lowValue}m`,
    //     font:'22px sans-serif',
    //     fillColor:Cesium.Color.CRIMSON
    //   }
    // })

    // this.lowEntity1 = this.viewer.entities.add({
    //   name:'2',
    //   position:Cesium.Cartesian3.fromDegrees(113.12281175835783,27.853440520627526,65),
    //   label:{
    //     text:  `${this.lowValue}m`,
    //     font:'22px sans-serif',
    //     fillColor:Cesium.Color.CRIMSON
    //   }
    // })

    this.yanmoEntity = this.viewer.entities.add({
      name:'2',
      position:Cesium.Cartesian3.fromDegrees(113.147163736578,27.898529129296623,100),
      label:{
        text:  ``,
        font:'25px sans-serif',
        fillColor:Cesium.Color.WHITE
      }
    })

    this.highPop = this.popDiv(Cesium.Cartesian3.fromDegrees(113.12425753259735,27.854658401763434,50),this.highPop)
    this.lowPop = this.popDiv(Cesium.Cartesian3.fromDegrees(113.12412411899265,27.853921627676886,50),this.lowPop)
    this.lowPop1 = this.popDiv(Cesium.Cartesian3.fromDegrees(113.12281175835783,27.853440520627526,50),this.lowPop1)
    $(this.highPop.element.children[0]).html(`${this.highValue}m`)
    $(this.lowPop.element.children[0]).html(`${this.lowValue}m`)
    $(this.lowPop1.element.children[0]).html(`${this.lowValue}m`)
    
    // this.shuiweiTimer = setInterval(() => {
    //   // 1是加 2是减
    //   this.updatePosition(1)
    // }, 2500);
    
  }

  popDiv( position, currOverlay) {
    if (currOverlay) {
        currOverlay.destroy();
    }
    let headdiv = `
          <div class="gif-pop">
            <div class="gif-text"></div>
            <div class="gif-div">
            </div>
          </div>`;
    
    let div = headdiv;
  
      let element = $(div)[0];
      let option = {
          element: element,
          position:position,
          offset: {
              x: 0,
              y: 0
          }
      };
    currOverlay = new Cesium.Overlay(option);
    this.viewer.addOverlay(currOverlay);
    return currOverlay
  }

  startShui(){
    //跳到指定位置,开启淹没
    this.viewer.camera.flyTo({
      destination: Cesium.Cartesian3.fromDegrees(113.14891217508294,27.897632553754608, 2416.6590013453415), //设置位置
      orientation: {
        heading: Cesium.Math.toRadians(311.1423209867936),
        pitch: Cesium.Math.toRadians(-89.99423260128289),
        roll: 0
      },
      duration:2
    });

    removeWaterSystem(this.viewer)

    this.shuiweiTimer&&clearInterval(this.shuiweiTimer)

    this.xiangjiangTimer&&clearInterval(this.xiangjiangTimer)
    
    this.destroyShuidongli()
    this.start5Ather()
    
  }

  destroyShuidongli(){
    this.waterEffect&&this.waterEffect.destroy()
    this.yanmoEntity.label._text._value = ``
  }

  updatePosition(type,callback = null){
    if(this.highPop){
      if(type === 1){
        this.highValue++
      } else {
        if(this.highValue !== 36){
          this.highValue--
        }
      }
      if(this.highValue === 41 || this.highValue === 36){
        clearInterval(this.shuiweiTimer)
        if(this.xiangjiangTimer){

          $(this.lowPop.element.children[0]).html(`44.46m`)
          $(this.lowPop1.element.children[0]).html(`44.46m`)

          clearInterval(this.xiangjiangTimer)
        }
        if(this.highValue === 36){
            closeGate(this.viewer,[0,1,2,3,4,5,6,7])
            removeWaterSystem(this.viewer)
            if(callback){
              callback()
            }
        }
      }
      
      $(this.highPop.element.children[0]).html(`${this.highValue}m`)
    }
  }


  start5Ather(){

    const drawInterval = 30
    this.waterEffect = new Cesium.WaterFloodSimulation(this.viewer,{showFlowField:true});
    this.waterEffect.loadFile(
      this.type === 0 ? {
        Grid: "data/水动力/have/gridData_2019_haveGate.json",
        Info: {
            Depth: "data/水动力/have/depth_2019_haveGate.csv",
            Stage: "data/水动力/have/stage_2019_haveGate.csv",
            U: "data/水动力/have/u_2019_haveGate.csv",
            V: "data/水动力/have/v_2019_haveGate.csv",
        }
     } : {
      Grid: "data/水动力/no/gridData_2019_noGate.json",
        Info: {
            Depth: "data/水动力/no/depth_2019_noGate.csv",
            Stage: "data/水动力/no/stage_2019_noGate.csv",
            U: "data/水动力/no/u_2019_noGate.csv",
            V: "data/水动力/no/v_2019_noGate.csv",
        }
     },
      {
          colorTable3:'img/colorTable4.png',
          minCameraHeight:300,
          rectPositions:[111.693258,28.800846,114.912632,28.829140,114.508624,26.865031,111.814421,26.924976,111.693258,28.800846],
          gridIdIndex:0,
          gridId:'单元编号',
          showFlowField: true,
          flowLineHeight:50,
          // 水面反射绘制
          reflectPlaneHeight: 1,
          reflectPlaneCenter: true,
          renderMode: 1,
          renderPass: [2],
          // 水面折射绘制 修改为copy当前帧的颜色纹理 提升性能
          renderMode2: 1,
          renderPass2: [],

          // 绘制类型 1 水面  2 水体
          drawType: 1,
          // 默认绘制批次 默认值 0 第一批
          drawIndex: 0, // 默认绘制第100批数据
          // 绘制批次的间隔 数据为多少分钟一批 这里数据5分钟/1批 内部会做颜色插值和透明过渡 更平滑
          drawInterval: drawInterval,
          // drawColors: "./data/colorTable2.png",
          // drawColors2: "./data/colorTable.png",

          // 水深的min和max阈值 用于渲染颜色
          maxHeight: 3,
          minHeight: 0,

          // gpu纹理大小 默认支持512*512=>262144网格数据 不够可以增加到1024 最大4096 需要考虑机器web的上限和实际性能
          texWidth: 512,
          texHeight: 512,
          // 水面高度 默认值-1 渲染当前批次数据最后一组的水位高度 避免重复绘制影响性能
          waterHeight: 48,
          // 渲染水位顶点动画 大场景慎用 0 不渲染属性高度  1 渲染属性高度 2 两倍渲染属性高度 以此类推
          renderHeight: 0,
          // 水面偏移高度 默认值为 0  在原始高度上偏移
          offsetHeight: 0,
          // 无效值过滤 默认值为0  当前网格水深为0时渲染透明
          isAlpha: -1,

          // 透明插值 前后两批数据通过播放进行透明度插值 增加推演多动态 批次之间也记录了前后两批数据做颜色插值 效果更平滑
          alphaLerp: true,

          startTime: Cesium.JulianDate.fromIso8601("2024-07-07T21:00Z"),
          stopTime: Cesium.JulianDate.fromIso8601("2024-07-15T23:00Z"),

      });

    this.waterEffect.loadEndEvent.addEventListener(() => {
        // 配置当前水面
        this.waterEffect.appearance.material.uniforms.u_baseColorType = 0; // 两种水面混合
        this.waterEffect.appearance.material.uniforms.oceanOneOverAmplitudeLowAltitude = 0.41; // 浪高
        this.waterEffect.appearance.material.uniforms.oceanFrequencyLowAltitude = 1500; // 密度
        this.waterEffect.appearance.material.uniforms.u_waterSpeed = 200; // 水流速
        this.waterEffect.appearance.material.uniforms.u_causticIntersectionFade = 3.25;  // 焦散缩放
        this.waterEffect.appearance.material.uniforms.u_causticScale = 0.17;  // 焦散缩放
        this.waterEffect.appearance.material.uniforms.u_FoamScale = 2.29; // 白浪缩放
        this.waterEffect.appearance.material.uniforms.u_FoamIntersectionFade = 1.96; // 白浪强度
        this.waterEffect.appearance.material.uniforms.u_WaterDeepColorAlpha = 0.65; // 深水透明度
        this.waterEffect.appearance.material.uniforms.luminance = 0.4;
        this.waterEffect.appearance.material.uniforms.baseColor = Cesium.Color.fromCssColorString('#c18e2e');

        this.waterEffect.alpha = 0.6;

        this.waterEffect.showFlowField = false;
        this.waterEffect.isAlpha = 0.0;

        this.waterEffect.alphaLerp = true;
        this.waterEffect._waterPrimitive.show = false;

        setTimeout(() => {
          this.waterEffect._waterPrimitive.show = true;
          this.waterEffect.forward(400)
        }, 1000);

    })

      //监听每个更新批次
      this.waterEffect.palyEvent.addEventListener((batchIndex, minutes) => {
        this.yanmoEntity.label._text._value = `${Math.floor(minutes / 60)}小时`
      })

  }

  start6(callback){
   
    //跳到闸口 开闸  降水位
      this.viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(113.12311596257716,27.85153334877819, 186.43181905182593), //设置位置
        orientation: {
          heading: Cesium.Math.toRadians(9.938829214334087),
          pitch: Cesium.Math.toRadians(-29.99506549199234),
          roll: 0
        },
        complete:(()=>{
          if(this.type === 0){
            setTimeout(() => {
              //关闭水闸
            closeGate(this.viewer,[0,1,2,3])
            }, 1500);
            setTimeout(() => {
              //打开水泵
            openGate(this.viewer,[4,5,6,7])
            }, 2500);
          }
        })
      });

      this.shuiweiTimer&&clearInterval(this.shuiweiTimer)
      this.xiangjiangTimer&&clearInterval(this.xiangjiangTimer)
      
      if(this.type === 0){
        downArea()
        createFlood(this.viewer);

        this.highValue = 41
        $(this.highPop.element.children[0]).html(`${this.highValue}m`)
        $(this.lowPop.element.children[0]).html(`${this.lowValue}m`)
        $(this.lowPop1.element.children[0]).html(`${this.lowValue}m`)
  
        this.shuiweiTimer = setInterval(() => {
          // 1是加 2是减
          this.updatePosition(2,callback)
        }, 3000);
        
        //湘江水位增加 
        this.xiangjiangTimer = setInterval(()=>{
          this.lowValue = (Number(this.lowValue) + 0.03).toFixed(2)
          $(this.lowPop.element.children[0]).html(`${this.lowValue}m`)
          $(this.lowPop1.element.children[0]).html(`${this.lowValue}m`)
        },200)
      }

  }

  removeShui(){

    this.highPop&&this.highPop.destroy()
    this.lowPop&&this.lowPop.destroy()
    this.lowPop1&&this.lowPop1.destroy()
    this.shuiweiTimer&&clearInterval(this.shuiweiTimer)
    this.xiangjiangTimer&&clearInterval(this.shuiweiTimer)
    this.yanmoEntity&&this.viewer.entities.remove(this.yanmoEntity)
    this.waterEffect&&this.waterEffect.destroy()
    removeWaterSystem(this.viewer)

  }

  clear(){

    // this.highEntity&&this.viewer.entities.remove(this.highEntity)
    // this.lowEntity&&this.viewer.entities.remove(this.lowEntity)
    // this.lowEntity1&&this.viewer.entities.remove(this.lowEntity1)
    this.yanmoEntity&&this.viewer.entities.remove(this.yanmoEntity)
    this.waterEffect&&this.waterEffect.destroy()
   
    this.shuiweiTimer&&clearInterval(this.shuiweiTimer)
    this.xiangjiangTimer&&clearInterval(this.shuiweiTimer)

    
    this.highPop&&this.highPop.destroy()
    this.lowPop&&this.lowPop.destroy()
    this.lowPop1&&this.lowPop1.destroy()
    removeWaterSystem(this.viewer)


    removeGate(this.viewer)
    clearAll()
  }

}